package com.koron.css2.contractmanage.impl;

import com.koron.css2.ExportPdfInterface;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.bean.ContractManageBean;
import com.koron.css2.contractmanage.exception.ContractException;
import com.koron.css2.contractmanage.service.EcContratServiceImpl;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.swan.bean.MessageBean;

import java.net.URLEncoder;

/**
 * 电子合同预览
 *
 */
public class EcContractPreview implements ExportPdfInterface {

    @Autowired
    private EcContratServiceImpl ecContratService = new EcContratServiceImpl();

    @Override
    public HttpEntity<?> exportPdf(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ContractManageBean queryBean = JsonUtils.objectToPojo(req.getData(), ContractManageBean.class);
            checkContractManageBean(userInfo.getCurWaterCode(), queryBean.getUserNo(), queryBean.getProjectId(),
                    queryBean.getContractNo(), queryBean.getContractType(), queryBean.getFileId());

            byte[] bytes = ecContratService.ecContractDownLoad(Long.valueOf(queryBean.getFileId()));
            String prefix = "供用水合同";
            String suffix = ".pdf";

            HttpHeaders header = new HttpHeaders();
            String prefixName= URLEncoder.encode(prefix,"UTF-8");
            header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            header.setContentDispositionFormData("attachment", prefixName + suffix);
            return new ResponseEntity<byte[]>(bytes, header, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("合同预览失败: " + e.getMessage(), e);
            e.printStackTrace();
            MessageBean<Object> messageBean = MessageBean.create(Constant.MESSAGE_INT_FAIL, "合同预览失败: " + e.getMessage(), null);
            ResponseEntity responseEntity = new ResponseEntity(messageBean, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }
    /**
     * 校验合同字段
     *
     * @param curWaterCode
     * @param userNo
     * @param projectId
     * @param contractType
     * @return
     */
    public void checkContractManageBean(String curWaterCode, String userNo, String projectId, String contractNo, String contractType, String fileId) {
        if (StringUtils.isBlank(curWaterCode)) {
            throw new ContractException("当前用户未绑定水司！");
        }
        if (StringUtils.isBlank(projectId)) {
            throw new ContractException("参数中的报装ID不能为空！");
        }
        if (StringUtils.isBlank(userNo)) {
            throw new ContractException("参数中的户号不能为空！");
        }
        if (StringUtils.isBlank(contractNo)) {
            throw new ContractException("参数中的合同编号不能为空！");
        }
        if (StringUtils.isBlank(contractType)) {
            throw new ContractException("参数中的合同类型不能为空！");
        }
        if (!ContractTypeEnum.CONTRACT_TYPE_2.getContractType().equals(contractType)) {
            throw new ContractException("参数中的合同类型不属于电子施工合同的类型！");
        }
        if (StringUtils.isBlank(fileId)) {
            throw new ContractException("参数中的合同文件ID不能为空！");
        }
    }

}
